package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.PoliceCallName;
import com.enjoyor.soa.traffic.core.tsms.mapper.IPoliceCallNameMapper;
import com.enjoyor.soa.traffic.core.tsms.service.IPoliceCallNameService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class PoliceCallNameService implements IPoliceCallNameService{

	@Autowired
	private IPoliceCallNameMapper iPoliceCallNameMapper;
	
	@Autowired
	private IPubLogMapper iPubLogMapper;
	
	@Override
	public Page getPoliceCallNamePage(Page page,String postId,String policeId,String callId,String executeTaskId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("postId", postId);
		map.put("policeId",policeId );
		map.put("callId",callId );
		map.put("executeTaskId",executeTaskId );
		page.setMapBean(map);
		return iPoliceCallNameMapper.getPoliceCallNamePage(page);
	}

	@Override
	public List<PoliceCallName> getPoliceCallNameList(String postId,String policeId,String callId,String executeTaskId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("postId", postId);
		map.put("policeId",policeId );
		map.put("callId",callId );
		map.put("executeTaskId",executeTaskId );
		return iPoliceCallNameMapper.getPoliceCallNameList(map);
	}

	@Override
	public void addPoliceCallName(PoliceCallName policeCallName, String personName) {
		// TODO Auto-generated method stub
		//单项执行任务、单项岗位的单个警员有且仅有唯一的点名记录信息；
		Map<String, Object> map = new HashMap<>();
		map.put("postId",policeCallName.getPostId() );
		map.put("policeId",policeCallName.getPoliceId());
		map.put("executeTaskId",policeCallName.getExecuteTaskId());
		//根据唯一条件确定之后已经有点名记录；
		List<PoliceCallName> list = iPoliceCallNameMapper.getPoliceCallNameList(map);
		//如果没有点名记录，则追加一条点名记录；
		if(list.size()==0){
			policeCallName.setCallId("PC"+TimeHelper.getUniqueString());
			policeCallName.setCallTime(TimeHelper.dateToLongString());
			policeCallName.setCallPerson(personName);
			iPoliceCallNameMapper.insertSelective(policeCallName);
			iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
					String.format("添加点名信息:{%s} 操作成功!", new Object[] { policeCallName.getId() }),
					LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_CALL_NAME_MANAGEMENT.getMessage());
		//如果已经有点名记录，则修改之前的记录；	
		}else{
			String updateCallId = list.get(0).getCallId();
			policeCallName.setCallId(updateCallId);
			policeCallName.setCallTime(TimeHelper.dateToLongString());
			policeCallName.setCallPerson(personName);
			iPoliceCallNameMapper.updateById(policeCallName);
			iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
					String.format("修改点名信息:{%s} 操作成功!", new Object[] { policeCallName.getId() }),
					LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_CALL_NAME_MANAGEMENT.getMessage());
		}
	}

	@Override
	public void editPoliceCallName(PoliceCallName policeCallName, String personName) {
		// TODO Auto-generated method stub
		iPoliceCallNameMapper.updateById(policeCallName);
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
				String.format("修改点名信息:{%s} 操作成功!", new Object[] { policeCallName.getId() }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_CALL_NAME_MANAGEMENT.getMessage());
	}

	@Override
	public void deletePoliceCallName(String callId, String personName) {
		// TODO Auto-generated method stub
		iPoliceCallNameMapper.deleteById(callId);
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
				String.format("删除点名信息:{%s} 操作成功!", new Object[] {callId}),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_POLICE_CALL_NAME_MANAGEMENT.getMessage());
	}

}
